package com.tms.monitoring.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.Random;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.tms.monitoring.domain.Exmanagement;
import com.tms.monitoring.service.IExmanagementService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 异常管理Controller
 *
 * @author ruoyi
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/exmanagement")
@CrossOrigin
public class ExmanagementController extends BaseController
{
    @Autowired
    private IExmanagementService exmanagementService;

    /**
     * 查询异常管理列表
     */
    @RequiresPermissions("monitoring:exmanagement:list")
    @GetMapping("/list")
    public TableDataInfo list(Exmanagement exmanagement)
    {
        startPage();
        List<Exmanagement> list = exmanagementService.selectExmanagementList(exmanagement);
        return getDataTable(list);
    }

    /**
     * 导出异常管理列表
     */
    @RequiresPermissions("monitoring:exmanagement:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Exmanagement exmanagement)
    {
        List<Exmanagement> list = exmanagementService.selectExmanagementList(exmanagement);
        ExcelUtil<Exmanagement> util = new ExcelUtil<Exmanagement>(Exmanagement.class);
        util.exportExcel(response, list, "异常管理数据");
    }

    /**
     * 获取异常管理详细信息
     */
    @RequiresPermissions("monitoring:exmanagement:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(exmanagementService.selectExmanagementById(id));
    }

    /**
     * 新增异常管理
     */
    @RequiresPermissions("monitoring:exmanagement:add")
    @PostMapping
    public AjaxResult add(@RequestBody Exmanagement exmanagement)
    {

        String datePart = new SimpleDateFormat("yyyyMMdd").format(new Date());
        int randomNum = new Random().nextInt(999999);
        exmanagement.setExceptionNumber("EX" + datePart + String.format("%06d", randomNum));
        exmanagement.setCustomer("七尾云科技有限公司");
        exmanagement.setOrderStatus("待审核");
        exmanagement.setCarrier("广汇物流有限公司");
        exmanagement.setDriver("林冲");
        StringBuilder phoneNumberBuilder = new StringBuilder("138");
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            phoneNumberBuilder.append(random.nextInt(10));
        }
        exmanagement.setPhoneNumber(phoneNumberBuilder.toString());
        exmanagement.setLicensePlate("京A88888888");
        exmanagement.setReporter("李四");
        exmanagement.setReportTime(new Date());
        exmanagement.setProcessingStatus("待处理");
        return toAjax(exmanagementService.insertExmanagement(exmanagement));
    }

    /**
     * 修改异常管理
     */
    @RequiresPermissions("monitoring:exmanagement:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody Exmanagement exmanagement)
    {
        exmanagement.setProcessingStatus("已完成");
        exmanagement.setProcessor("李四");
        exmanagement.setProcessingTime(new Date());
        exmanagement.setMeasuresTaken("联系承运商进行赔偿");
        return toAjax(exmanagementService.updateExmanagement(exmanagement));
    }

    /**
     * 删除异常管理
     */
    @RequiresPermissions("monitoring:exmanagement:remove")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(exmanagementService.deleteExmanagementByIds(ids));
    }

    @RequestMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable Long id){
        try {
            Exmanagement exmanagement = exmanagementService.selectExmanagementById(id);
            exmanagement.setProcessingStatus("已完成");
            return R.ok(exmanagement);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("修改失败");
    }
}
